using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using ShootThemUp.Sources.GameElements;

namespace ShootThemUp.Sources.GameElements.Ships.Player
{
    /// <summary>
    /// This class represents the reactor of a ship. It's only visual, it have no effect on the model.
    /// </summary>
    public class Reactor : MovableSprite 
    {
        #region Spritesheet Constant Fields

        /// <summary>
        /// The x position of the texture of the reactor in the spritesheet.
        /// </summary>
        private const int SPRITESHEET_POSITION_X = 0;
        /// <summary>
        /// The y position of the texture of the reactor in the spritesheet.
        /// </summary>
        private const int SPRITESHEET_POSITION_Y = 36;
        /// <summary>
        /// The width of the texture of the reactor in the spritesheet.
        /// </summary>
        private const int SPRITESHEET_WIDTH = 42;
        /// <summary>
        /// The height of the texture of the reactor in the spritesheet.
        /// </summary>
        private const int SPRITESHEET_HEIGHT = 35;
        /// <summary>
        /// The number of sprites used to animate the reactor.
        /// </summary>
        private const int NUMBER_OF_FRAME = 2;
        /// <summary>
        /// The duration of a frame of the animation.
        /// </summary>
        private const float ANIMATION_DURATION = 0.008f;

        #endregion

        #region Fields

        /// <summary>
        /// The index of the current frame. Used to animate the texture.
        /// </summary>
        private float animationIndex = 0;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Reactor"/> class.
        /// </summary>
        public Reactor(Vector2 position) : base(position, Vector2.Zero, new Rectangle( Reactor.SPRITESHEET_POSITION_X, Reactor.SPRITESHEET_POSITION_Y, Reactor.SPRITESHEET_WIDTH, Reactor.SPRITESHEET_HEIGHT))
        {
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the sprite position in function of the specified game time.
        /// </summary>
        /// <param name="gameTime">A snapshot of the current time.</param>
        public override void Update(GameTime gameTime)
        {
            this.animationIndex += gameTime.ElapsedGameTime.Milliseconds * Reactor.ANIMATION_DURATION; 

            if (animationIndex > Reactor.NUMBER_OF_FRAME)
                animationIndex = 0;

            this.SourceRectangle = new Rectangle((int)this.animationIndex * Reactor.SPRITESHEET_WIDTH, Reactor.SPRITESHEET_POSITION_Y, Reactor.SPRITESHEET_WIDTH, Reactor.SPRITESHEET_HEIGHT);

            base.Update(gameTime);
        }

        #endregion
    }
}
